package com.qqtech.qquan.community.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityYellow;
import com.qqtech.qquan.community.model.CommunityYellowQuery;
import com.qqtech.qquan.community.model.CommunityYellowVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityYellowService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/community/yellow")
public class CommunityYellowController extends BaseAgentController {

	@Resource
	private CommunityYellowService communityYellowService;
	
	@Resource
	private CommunityBaseService communityBaseService;
	
	@Override
	protected BaseService<CommunityYellow> getBaseService() {
		return communityYellowService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/community/communityYellow-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CommunityYellowQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		query.setCommunityId(super.getCurrCommunityId());//当前后台用户的社区id
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityYellowVo> page = communityYellowService.queryPageList(query, pageKit);
		CommunityBaseVo communityBaseVo=null;
		for (CommunityYellowVo communityYellowVo : page) {
			communityBaseVo=communityBaseService.getById(communityYellowVo.getCommunityId());
			communityYellowVo.setCommunityName(communityBaseVo.getName());
		}
		adminPage.setRows(page.getContent());
		//adminPage.setTotal(page.getNumberOfElements());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	/**
	 * 鍘绘柊澧�
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		CommunityBaseQuery communityBaseQuery=new CommunityBaseQuery();
		communityBaseQuery.setId(super.getCurrCommunityId());//当前后台用户的社区id
		List<CommunityBaseVo> communityList=communityBaseService.queryList(communityBaseQuery);
		model.addAttribute("communityList", communityList);
		return "/community/communityYellow-add";
	}
	
	/**
	 * 鍒涘缓鎴愬姛
	 * 
	 * @param add
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(CommunityYellow communityYellow) {
		Result ret = new Result();
		communityYellowService.insert(communityYellow);
		return ret;
	}
	
	/**
	 * 鍘绘洿鏂�
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		CommunityYellowVo communityYellowVo = communityYellowService.getById(id);
		model.addAttribute("communityYellowVo", communityYellowVo);
		CommunityYellowQuery communityYellowQuery = new CommunityYellowQuery();
		CommunityBaseQuery communityBaseQuery=new CommunityBaseQuery();
		communityBaseQuery.setId(super.getCurrCommunityId());//当前后台用户的社区id
		List<CommunityBaseVo> communityList=communityBaseService.queryList(communityBaseQuery);
		List<CommunityYellowVo> list = communityYellowService.queryList(communityYellowQuery);
		model.addAttribute("list", list);
		model.addAttribute("communityList", communityList);
		return "/community/communityYellow-update";
	}
	
	/**
	 * 鍒涘缓鎴愬姛
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(CommunityYellow communityYellow) {
		Result ret = new Result();
		communityYellowService.updateById(communityYellow	);
		return ret;
	}
	
	/**
	 * 鍒犻櫎
	 * 
	 * @param del
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(Integer id) {
		Result ret = new Result();
		communityYellowService.deleteById(id);
		return ret;
	}
}
